﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dosimetria.Models;
using Dosimetria.DAL;
using Dosimetria.Models.DataAnnotations_Classes;

namespace Dosimetria.Controllers
{ 
    public class RolController : Controller
    {
        private Querys db = new Querys();
        //
        // GET: /Rol/

        public ViewResult Index()
        {
            String query = "select * from roles";
            DataTable tabla =  Conexion.getSelectTable(query);

            List<Rol> lRoles = new List<Rol>();
            foreach(DataRow fila in tabla.Rows)
            {
                Rol r = new Rol();
                r.id = Convert.ToInt32(fila["id"]);
                r.nombre = fila["nombre"].ToString();
                r.vigencia = Convert.ToBoolean(fila["vigencia"]);
                lRoles.Add(r);
            }
            return View(lRoles);
        }

        //
        // GET: /Rol/Details/5

        public ViewResult Details(int id)
        {
            string query = "select * from roles " +
                            "where r.id = @rolID ";
            DataTable tabla = Conexion.getSelectTable(query, id);
            List<Rol> lRol = new List<Rol>();
            foreach(DataRow fila in tabla.Rows)
            {
                Rol r = new Rol();
                r.id = Convert.ToInt32(fila["id"]);
                r.nombre = fila["nombre"].ToString();
                r.vigencia = Convert.ToBoolean(fila["vigencia"]);
            }

            Rol r1 = null;
            try
            {
                r1 = lRol[0];
            }
            catch(Exception e)
            {
                
            }
            return View(r1);
        }

        //
        // GET: /Rol/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Rol/Create

        [HttpPost]
        public ActionResult Create(Rol roles)
        {
            if (ModelState.IsValid)
            {
                db.createtRol(roles);
                return RedirectToAction("Index");  
            }

            return View(roles);
        }
        
        //
        // GET: /Rol/Edit/5
 
        //public ActionResult Edit(int id)
        //{
        //    roles roles = db.roles.Single(r => r.rol_id == id);
        //    return View(roles);
        //}

        ////
        //// POST: /Rol/Edit/5

        //[HttpPost]
        //public ActionResult Edit(roles roles)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.roles.Attach(roles);
        //        db.ObjectStateManager.ChangeObjectState(roles, EntityState.Modified);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(roles);
        //}

        ////
        //// GET: /Rol/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    roles roles = db.roles.Single(r => r.rol_id == id);
        //    return View(roles);
        //}

        ////
        //// POST: /Rol/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{            
        //    roles roles = db.roles.Single(r => r.rol_id == id);
        //    db.roles.DeleteObject(roles);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    db.Dispose();
        //    base.Dispose(disposing);
        //}
    }
}